
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author BENSON
 */
public class ControllerAddEmp {
     
    Connection conn=null;
    String querry;
    Statement st=null;
    public EnterEmployee enterEmp;
    private ActionListener actionListener;
    
    public ControllerAddEmp( ){
       // this.deleteOrder = delete;
        enterEmp = new EnterEmployee();
        enterEmp.setVisible(true);
                          
    }
  
   
    public void addEmployee(){
        try{
        Class.forName("com.mysql.jdbc.Driver");
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
       
        querry=("INSERT INTO employees SET eId=?,eName=?,email=?,password=?;") ;
       PreparedStatement p =conn.prepareStatement(querry);
       p.setString(1,enterEmp.getEmpId().getText());
       p.setString(2,enterEmp.getEmpName().getText());
       p.setString(3,enterEmp.getEmpEmail().getText());
       p.setString(4,enterEmp.getEmpPass().getText());
       p.execute();
       
        p.close();
        conn.close();
        JOptionPane.showMessageDialog(null,"Employee has been added");
        }
        catch(Exception e){
         JOptionPane.showMessageDialog(null,"Error: "+e);
        }
    }
    
     public void contol(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==enterEmp.getSaveButton()){
                        addEmployee();
                  }
                  if(actionEvent.getSource()==enterEmp.getCloseButton()){
                        enterEmp.setVisible(false);
                  }
                  }
              
        };                
        enterEmp.getSaveButton().addActionListener(actionListener);
        enterEmp.getCloseButton().addActionListener(actionListener);
        }
     public static void main(String[]args){
         ControllerAddEmp add= new ControllerAddEmp();
         add.contol();
     
     }
    
}
